package com.bobby.shop.service.impl;

import com.bobby.shop.mapper.ArticleMapper;
import com.bobby.shop.mapper.ArticleTypeMapper;
import com.bobby.shop.pojo.ArticleType;
import com.bobby.shop.pojo.ArticleTypeExample;
import com.bobby.shop.service.ArticleService;
import com.bobby.shop.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Date 2021/1/4 19:17
 * @Created wenjunpei
 */
@Service
@Transactional
public class ArticleTypeServiceImpl implements ArticleTypeService {

    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Override
    public List<ArticleType> findArticleTypeByCode(String code) {
        ArticleTypeExample articleTypeExample = new ArticleTypeExample();
        ArticleTypeExample.Criteria criteria = articleTypeExample.createCriteria();
        if(code.length() == 8) {
            criteria.andCodeLike(  code.substring(0,4) + "%");
        }else {
            criteria.andCodeLike(code + "%");
        }
        List<ArticleType> articleTypeList = articleTypeMapper.selectByExample(articleTypeExample);
        return articleTypeList;
    }

    @Override
    public List<ArticleType> findAll() {
        List<ArticleType> articleTypeList = articleTypeMapper.selectByExample(new ArticleTypeExample());
        List<ArticleType> targetList = new ArrayList<>();
        for (ArticleType articleType : articleTypeList) {
            if(!articleType.getName().contains("-")) {
                targetList.add(articleType);
            }
        }
        return targetList;
    }
}
